home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Documents / JAVA Programming / examples / 10 / ExceptionDemo.java < prev    next >
Encoding:
Java Source  |  2000-09-08  |  527 b   |  28 lines

  1. class MyException extends Exception {
  2. private int detail;
  3. MyException(int a) {
  4. detail = a;
  5. }
  6. public String toString() {
  7. return "MyException[" + detail + "]";
  8. }
  9.  
  10. class ExceptionDemo {
  11. static void compute(int a) throws MyException {
  12. System.out.println("called compute(" + a + ").");
  13. if (a > 10)
  14. throw new MyException(a);
  15. System.out.println("normal exit.");
  16. }
  17.  
  18. public static void main(String args[]) {
  19. try {
  20. compute(1);
  21. compute(20);
  22. }
  23. catch (MyException e) {
  24. System.out.println("caught" + e);
  25. }
  26. } } 
  27.